package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.entity.CountBean;
import com.example.demo.mapper.CountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("index")
public class IndexAllController {
    /**
     * 来到主页并进行渲染
     */
    @Autowired
    CountMapper countMapper;

    @RequestMapping("toIndex")
    public ModelAndView toIndex(ModelAndView modelAndView) {
        Map<String, Integer> map = new HashMap<>();
        for (CountBean cb : countMapper.selectAllCount()) {
            map.put(cb.getName(), cb.getNum());
        }
        modelAndView.addObject("allcount", JSON.toJSON(map));
        for (CountBean cb : countMapper.selectDayCount(1)) {
            map.put(cb.getName(), cb.getNum());
        }
        modelAndView.addObject("onedaycount", JSON.toJSON(map));
        for (CountBean cb : countMapper.selectDayCount(7)) {
            map.put(cb.getName(), cb.getNum());
        }
        modelAndView.addObject("sevendaycount", JSON.toJSON(map));
        for (CountBean cb : countMapper.selectDayCount(30)) {
            map.put(cb.getName(), cb.getNum());
        }
        modelAndView.addObject("monthdaycount", JSON.toJSON(map));
        modelAndView.setViewName("Index");
        return modelAndView;
    }


}
